/* 
 * File:   main.cpp
 * Author: mrossetti
 *
 * Created on 22 giugno 2011, 15.46
 */

#include <cstdlib>
#include "main.h"

using namespace std;

/*
 * 
 */

int main(int argc, char** argv) {
    Main ProvaSDL;
    return ProvaSDL.OnExecute();
}

Main::Main(){
    CurrentPlayer = 0;
    Surf_Display = NULL;
    Surf_Grid = NULL;
    Surf_O = NULL;
    Surf_X = NULL;
    Surf_text = NULL;
    Surf_text_winner = NULL;
    Running = true;
    GameEnded = false;
    isFree = false;
    EndDisplayed = false;
    Players[0] = "Player 1!!!";
    Players[1] = "Player 2!!!";
    Winners[0] = "Player 1 WIN!!!";
    Winners[1] = "Player 2 WIN!!!";
}

int Main::OnExecute(){
    if(OnInit() == false){
        return -1;
    }

    SDL_Event Event;

    while(Running){
        while(SDL_PollEvent(&Event)){
            OnEvent(&Event);
        }
        OnLoop();
        OnRender();
    }
    OnCleanup();
    return 0;
}

void Main::Reset(){
    for(int i = 0;i < 9;i++){
        Grid[i] = GRID_TYPE_NONE;
    }
    CurrentPlayer = 0;
    GameEnded = false;
    isFree = false;
    EndDisplayed = false;
    if(Surf_text_winner != NULL){
        SDL_FreeSurface(Surf_text_winner);
    }
    Surf_text_winner = NULL;
    ChangePlayer();
}

void Main::SetCell(int ID, int Type) {

    ScanGrid4FreeSpace();
    ScanGrid4Winner();
    
    if(!isFree || GameEnded){
        if(!EndDisplayed){
            DisplayWinner();
            return;
        }else{
            Reset();
            return;
        }
    }else{
        if(Grid[ID] != GRID_TYPE_NONE) {
            return;
        }
    }
    
    if(ID < 0 || ID >= 9) return;
    if(Type < 0 || Type > GRID_TYPE_O) return;
    Grid[ID] = Type;
    
    ScanGrid4FreeSpace();
    ScanGrid4Winner();
    
    if(!isFree || GameEnded){
        if(!EndDisplayed){
            DisplayWinner();
        }
    }
}

void Main::ChangePlayer(){
    if(Surf_text != NULL){
        SDL_FreeSurface(Surf_text);
        if((Surf_text = CSurface::OnFontLoad("img/fonts/arial.ttf", Players[CurrentPlayer])) == NULL){
            exit(1);
        }
    }
}

void Main::DisplayWinner(){
    char* Msg = "";
    if(Surf_text_winner != NULL){
        SDL_FreeSurface(Surf_text_winner);
    }
    
    if(isFree){
        Msg = Winners[CurrentPlayer];
    }else{
        Msg = "Draw Game!";
    }
    if((Surf_text_winner = CSurface::OnFontLoad("img/fonts/arial.ttf", Msg)) == NULL){
        exit(1);
    }
    EndDisplayed = true;
}

void Main::ScanGrid4FreeSpace(){
    isFree = false;
    for(int k = 0;k<9;k++){
        if(Grid[k] == GRID_TYPE_NONE)isFree = true;
    }
}

void Main::ScanGrid4Winner(){
    //orizzontali
    if(( ((Grid[0] == Grid[1]) && (Grid[0] == Grid[2]) && (Grid[1] == Grid[2])) && (Grid[0] + Grid[1] + Grid[2] >= 3)) ||
       ( ((Grid[3] == Grid[4]) && (Grid[3] == Grid[5]) && (Grid[4] == Grid[5])) && (Grid[3] + Grid[4] + Grid[5] >= 3)) ||
       ( ((Grid[6] == Grid[7]) && (Grid[6] == Grid[8]) && (Grid[7] == Grid[8])) && (Grid[6] + Grid[7] + Grid[8] >= 3)) ||
       //verticali
       ( ((Grid[0] == Grid[3]) && (Grid[0] == Grid[6]) && (Grid[3] == Grid[6])) && (Grid[0] + Grid[3] + Grid[6] >= 3)) ||
       ( ((Grid[1] == Grid[4]) && (Grid[1] == Grid[7]) && (Grid[4] == Grid[7])) && (Grid[1] + Grid[4] + Grid[7] >= 3)) ||
       ( ((Grid[2] == Grid[5]) && (Grid[2] == Grid[8]) && (Grid[5] == Grid[8])) && (Grid[2] + Grid[5] + Grid[8] >= 3)) ||
       //diagonali
       ( ((Grid[0] == Grid[4]) && (Grid[0] == Grid[8]) && (Grid[4] == Grid[8])) && (Grid[0] + Grid[4] + Grid[8] >= 3)) ||
       ( ((Grid[2] == Grid[4]) && (Grid[2] == Grid[6]) && (Grid[4] == Grid[6])) && (Grid[2] + Grid[4] + Grid[6] >= 3)))GameEnded = true;
}

int Main::minimalAI(){
    int MyCell = -1;
    
    while(MyCell < 0){
        for(int i = 0;i<9;i++){
            if(Grid[0] == GRID_TYPE_X){
                if(Grid[1] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_NONE)MyCell = 2;
                }else if(Grid[2] == GRID_TYPE_X){
                    if(Grid[1] == GRID_TYPE_NONE)MyCell = 1;
                }else if(Grid[3] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }else if(Grid[6] == GRID_TYPE_X){
                    if(Grid[3] == GRID_TYPE_NONE)MyCell = 3;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_X)MyCell = 4;
                }
            }else if(Grid[1] == GRID_TYPE_X){
                if(Grid[2] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_NONE)MyCell = 0;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_NONE)MyCell = 2;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[7] == GRID_TYPE_NONE)MyCell = 7;
                }else if(Grid[7] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_NONE)MyCell = 4;
                }
            }else if(Grid[2] == GRID_TYPE_X){
                if(Grid[1] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_NONE)MyCell = 0;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[1] == GRID_TYPE_NONE)MyCell = 1;
                }else if(Grid[5] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[5] == GRID_TYPE_NONE)MyCell = 5;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }else if(Grid[6] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_X)MyCell = 4;
                }
            }else if(Grid[3] == GRID_TYPE_X){
                if(Grid[6] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_NONE)MyCell = 0;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[5] == GRID_TYPE_NONE)MyCell = 5;
                }else if(Grid[5] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_NONE)MyCell = 4;
                }
            }else if(Grid[4] == GRID_TYPE_X){
                if(Grid[1] == GRID_TYPE_X){
                    if(Grid[7] == GRID_TYPE_NONE)MyCell = 7;
                }else if(Grid[7] == GRID_TYPE_X){
                    if(Grid[1] == GRID_TYPE_NONE)MyCell = 1;
                }else if(Grid[5] == GRID_TYPE_X){
                    if(Grid[3] == GRID_TYPE_NONE)MyCell = 3;
                }else if(Grid[3] == GRID_TYPE_X){
                    if(Grid[5] == GRID_TYPE_NONE)MyCell = 5;
                }else if(Grid[2] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }else if(Grid[6] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_X)MyCell = 2;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_X)MyCell = 0;
                }
            }else if(Grid[5] == GRID_TYPE_X){
                if(Grid[4] == GRID_TYPE_X){
                    if(Grid[3] == GRID_TYPE_NONE)MyCell = 3;
                }else if(Grid[3] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_NONE)MyCell = 4;
                }else if(Grid[2] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_NONE)MyCell = 2;
                }
            }else if(Grid[6] == GRID_TYPE_X){
                if(Grid[7] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[7] == GRID_TYPE_NONE)MyCell = 7;
                }else if(Grid[3] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_NONE)MyCell = 0;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[3] == GRID_TYPE_NONE)MyCell = 3;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_NONE)MyCell = 2;
                }else if(Grid[2] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_X)MyCell = 4;
                }
            }else if(Grid[7] == GRID_TYPE_X){
                if(Grid[4] == GRID_TYPE_X){
                    if(Grid[1] == GRID_TYPE_NONE)MyCell = 1;
                }else if(Grid[1] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_NONE)MyCell = 4;
                }else if(Grid[6] == GRID_TYPE_X){
                    if(Grid[8] == GRID_TYPE_NONE)MyCell = 8;
                }else if(Grid[8] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }
            }else if(Grid[8] == GRID_TYPE_X){
                if(Grid[7] == GRID_TYPE_X){
                    if(Grid[6] == GRID_TYPE_NONE)MyCell = 6;
                }else if(Grid[6] == GRID_TYPE_X){
                    if(Grid[7] == GRID_TYPE_NONE)MyCell = 7;
                }else if(Grid[4] == GRID_TYPE_X){
                    if(Grid[0] == GRID_TYPE_NONE)MyCell = 0;
                }else if(Grid[0] == GRID_TYPE_X){
                    if(Grid[4] == GRID_TYPE_NONE)MyCell = 4;
                }else if(Grid[5] == GRID_TYPE_X){
                    if(Grid[2] == GRID_TYPE_NONE)MyCell = 2;
                }else if(Grid[2] == GRID_TYPE_X){
                    if(Grid[5] == GRID_TYPE_X)MyCell = 5;
                }
            }
        }
        if(MyCell < 0){
            for(int k = 0;k<9;k++){
                if(Grid[k] == GRID_TYPE_NONE)MyCell = k;
            }            
        }
        if(MyCell < 0)break;
    }
    
    return MyCell;
}